﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Display.TrainUI {
    import flash.events.*;
    import flash.display.*;
    import org.aswing.*;
    import Classes.*;
    import Classes.util.*;
    import Classes.sim.*;
    import com.greensock.*;
    import flash.filters.*;
    import org.aswing.geom.*;
    import ZLocalization.*;
    import Display.*;
    import Display.FactoryUI.*;
    import Classes.orders.*;
    import org.aswing.event.*;
    import Classes.inventory.*;
    import Display.aswingui.*;

    public class TrainSlidePick extends SlidePick {

        public static const SLIDE_WIDTH:int = 350;
        public static const BUTTON_HEIGHT:int = 25;
        public static const INFO_PANE_HEIGHT:int = 30;
        public static const STATE_CLOSED:int = 0;
        public static const STATE_ACCEPT:int = 1;
        public static const STATE_TOASTER:int = 2;

        protected static var m_assets:Object = null;

        protected var m_state:int;
        protected var m_info:JLabel;
        protected var m_info2:JLabel;
        protected var m_amountLabel:JLabel;
        protected var m_trainSim:TrainManager;
        protected var m_order:TrainOrder;
        protected var m_uiHandle:int;
        protected var m_amountToAccept:int;

        public function TrainSlidePick(_arg1:String, _arg2:int, _arg3:TrainManager, _arg4:TrainOrder, _arg5:Boolean=false, _arg6:Boolean=true, _arg7:int=0, _arg8:Boolean=false, _arg9:int=-1){
            this.m_order = _arg4;
            this.m_trainSim = _arg3;
            this.m_uiHandle = _arg2;
            this.m_info = null;
            this.m_amountLabel = null;
            this.m_amountToAccept = 0;
            this.m_state = STATE_CLOSED;
            super(_arg1, _arg5, _arg6, _arg7, false, SLIDE_WIDTH);
        }
        public function getPickSprite():Sprite{
            return (this.m_mainSprite);
        }
        override protected function getPickBackground():Class{
            return (EmbeddedArt.trainUIPick);
        }
        override protected function getInnerBackground():Class{
            var _local1:int;
            if (this.m_order.getState() == OrderStates.ACCEPTED){
                _local1 = (this.m_order.getAmountProposed() - this.m_order.getAmountFinal());
                if (_local1 > 0){
                    return (EmbeddedArt.trainUIBG2);
                };
            };
            return (EmbeddedArt.trainUIBG);
        }
        override protected function initInner():void{
            super.initInner();
            switch (this.m_state){
                case STATE_ACCEPT:
                    this.initAccept();
                    break;
                case STATE_TOASTER:
                    this.initToaster();
                    break;
            };
        }
        protected function initAccept():void{
            var _local8:AssetPane;
            var _local9:CustomButton;
            var _local10:CustomButton;
            var _local11:int;
            var _local12:JPanel;
            var _local13:AssetPane;
            var _local14:JLabel;
            var _local15:JLabel;
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(AsWingConstants.CENTER);
            _local1.setPreferredWidth(slideWidth);
            var _local2:JPanel = ASwingHelper.makeSoftBoxJPanel(AsWingConstants.CENTER);
            _local2.setPreferredHeight(INFO_PANE_HEIGHT);
            var _local3:JPanel = ASwingHelper.makeSoftBoxJPanel(AsWingConstants.CENTER, 15);
            this.m_info = new JLabel(this.getInfoText());
            this.m_info.setFont(ASwingHelper.getBoldFont(14));
            _local2.append(this.m_info);
            if (this.m_order.getCommodityType() == Commodities.GOODS_COMMODITY){
                _local8 = new AssetPane((new m_assets.crate_small() as DisplayObject));
                _local2.append(_local8);
            };
            var _local4:int;
            var _local5:CustomButton = new CustomButton(this.getAcceptButtonText(), null, "GreenSmallButtonUI");
            _local5.setPreferredHeight(BUTTON_HEIGHT);
            _local5.addActionListener(this.onAccept, 0, true);
            if (this.m_order.getState() == OrderStates.PENDING){
                _local4 = this.m_order.getCoinCostProposed();
                _local3.append(_local5);
                _local9 = new CustomButton(ZLoc.t("Dialogs", "TrainUI_Later"), null, "OrangeSmallButtonUI");
                _local9.setPreferredHeight(BUTTON_HEIGHT);
                _local9.addActionListener(this.onLater, 0, true);
                _local3.append(_local9);
                _local10 = new CustomButton(ZLoc.t("Dialogs", "TrainUI_Decline"), null, "RedSmallButtonUI");
                _local10.setPreferredHeight(BUTTON_HEIGHT);
                _local10.addActionListener(this.onDecline, 0, true);
                _local3.append(_local10);
            } else {
                _local4 = this.m_order.getCoinCostFinal();
                _local11 = (this.m_order.getAmountProposed() - this.m_order.getAmountFinal());
                if (_local11 > 0){
                    _local12 = ASwingHelper.makeSoftBoxJPanel(AsWingConstants.CENTER);
                    _local12.setPreferredWidth((slideWidth / 2));
                    _local12.setPreferredHeight(INFO_PANE_HEIGHT);
                    _local13 = null;
                    if (this.m_order.getAction() == TrainOrder.BUY){
                        _local11 = (this.m_order.getCoinCostProposed() - this.m_order.getCoinCostFinal());
                        _local13 = new AssetPane((new m_assets.coin_small() as DisplayObject));
                    } else {
                        if (this.m_order.getCommodityType() == Commodities.GOODS_COMMODITY){
                            _local13 = new AssetPane((new m_assets.crate_small() as DisplayObject));
                        } else {
                            _local13 = new AssetPane((new m_assets.crate_small() as DisplayObject));
                        };
                    };
                    _local14 = new JLabel(ZLoc.t("Dialogs", "TrainUI_YouGet", {number:_local11}));
                    _local14.setFont(ASwingHelper.getBoldFont(14));
                    _local15 = new JLabel(ZLoc.t("Dialogs", "TrainUI_Back"));
                    _local15.setFont(ASwingHelper.getBoldFont(14));
                    _local12.appendAll(_local14, _local13, _local15);
                    ASwingHelper.prepare(_local12);
                    _local3.appendAll(_local12, _local5);
                } else {
                    _local3.append(_local5);
                };
            };
            var _local6:JLabel = new JLabel(ZLoc.t("Dialogs", "TrainUI_ForNum", {number:_local4}));
            _local6.setFont(ASwingHelper.getBoldFont(14));
            _local2.append(_local6);
            var _local7:AssetPane = new AssetPane((new m_assets.coin_small() as DisplayObject));
            _local2.append(_local7);
            ASwingHelper.prepare(_local2);
            ASwingHelper.prepare(_local3);
            _local1.appendAll(_local2, _local3);
            ASwingHelper.prepare(_local1);
            innerAppendAll(_local1);
        }
        protected function initToaster():void{
            var _local6:CustomButton;
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(AsWingConstants.CENTER);
            _local1.setPreferredWidth(slideWidth);
            var _local2:JPanel = ASwingHelper.makeSoftBoxJPanel(AsWingConstants.CENTER);
            _local2.setPreferredHeight(INFO_PANE_HEIGHT);
            var _local3:JPanel = new JPanel(new BorderLayout());
            this.m_info = new JLabel(this.getInfoText());
            this.m_info.setFont(ASwingHelper.getBoldFont(14));
            this.m_info2 = new JLabel(this.getInfoText2());
            this.m_info2.setFont(ASwingHelper.getBoldFont(14));
            var _local4:AssetPane;
            if (this.m_order.getCommodityType() == Commodities.GOODS_COMMODITY){
                _local4 = new AssetPane((new m_assets.crate_small() as DisplayObject));
            } else {
                _local4 = new AssetPane((new m_assets.crate_small() as DisplayObject));
            };
            var _local5:AssetPane = new AssetPane((new m_assets.coin_small() as DisplayObject));
            _local2.appendAll(this.m_info, _local4, this.m_info2, _local5);
            var _local7:JPanel = ASwingHelper.makeSoftBoxJPanel();
            _local7.append(ASwingHelper.horizontalStrut(15));
            var _local8:CustomButton = new CustomButton("", null, "MinusButtonUI");
            _local8.setPreferredSize(new IntDimension(BUTTON_HEIGHT, BUTTON_HEIGHT));
            _local8.addActionListener(this.onLess, 0, true);
            _local7.append(_local8);
            var _local9:DisplayObject = new EmbeddedArt.trainUIAmntField();
            this.m_amountLabel = new JLabel(("" + this.m_amountToAccept));
            this.m_amountLabel.setFont(ASwingHelper.getBoldFont(16));
            this.m_amountLabel.setForeground(new ASColor(0xFFFFFF));
            this.m_amountLabel.setTextFilters([new DropShadowFilter(1.5, 90, 13204516, 1, 0, 0)]);
            this.m_amountLabel.setBackgroundDecorator(new AssetBackground(_local9));
            this.m_amountLabel.setPreferredSize(new IntDimension(_local9.width, _local9.height));
            _local7.append(this.m_amountLabel);
            var _local10:CustomButton = new CustomButton("", null, "PlusButtonUI");
            _local10.setPreferredSize(new IntDimension(BUTTON_HEIGHT, BUTTON_HEIGHT));
            _local10.addActionListener(this.onMore, 0, true);
            _local7.append(_local10);
            _local3.append(_local7, BorderLayout.WEST);
            var _local11:JPanel = ASwingHelper.makeSoftBoxJPanel();
            _local6 = new CustomButton(this.getAcceptButtonText(), null, "GreenSmallButtonUI");
            _local6.setPreferredHeight(BUTTON_HEIGHT);
            _local6.addActionListener(this.onAccept, 0, true);
            _local11.append(_local6);
            _local11.append(ASwingHelper.horizontalStrut(15));
            _local3.append(_local11, BorderLayout.EAST);
            ASwingHelper.prepare(_local2);
            ASwingHelper.prepare(_local3);
            _local1.appendAll(_local2, _local3);
            ASwingHelper.prepare(_local1);
            innerAppendAll(_local1);
        }
        protected function makeAssets(_arg1:DisplayObject, _arg2:String):void{
            m_assets = _arg1;
            this.toggleInnerPane();
        }
        public function showTrainUI(_arg1:TrainOrder):void{
            var _local2:AWEvent;
            var _local3:Number;
            var _local4:int;
            var _local5:int;
            this.m_order = _arg1;
            if (this.m_order.autoComplete){
                _local2 = new AWEvent("");
                this.onAccept(_local2);
                return;
            };
            if (this.m_order.getState() == OrderStates.PENDING){
                this.m_amountToAccept = _arg1.getAmountProposed();
                if (this.m_order.getAction() == TrainOrder.SELL){
                    _local3 = TrainOrder.getCostPerCommodity(_arg1.getCommodityType(), _arg1.getTrainItemName(), _arg1.getAction());
                    _local4 = Math.floor((Global.player.gold / _local3));
                    if (_local4 < this.m_amountToAccept){
                        this.m_amountToAccept = _local4;
                    };
                } else {
                    if (this.m_order.getAction() == TrainOrder.BUY){
                        _local5 = Global.player.commodities.getCount(this.m_order.getCommodityType());
                        if (this.m_amountToAccept > _local5){
                            this.m_amountToAccept = _local5;
                        };
                    };
                };
            };
            this.m_state = STATE_ACCEPT;
            if (m_assets == null){
                Global.delayedAssets.get(DelayedAssetLoader.TRAIN_ASSETS, this.makeAssets);
            } else {
                this.toggleInnerPane();
            };
        }
        protected function fn():LocalizationName{
            var _local1:String;
            var _local2:String = ((this.m_order.getTransmissionStatus() == OrderStatus.SENT)) ? this.m_order.getRecipientID() : this.m_order.getSenderID();
            _local1 = Global.player.getFriendFirstName(_local2);
            if (_local1 == null){
                _local1 = "???";
            };
            return (ZLoc.tn(_local1));
        }
        protected function getInfoText2():String{
            var _local1:int;
            if (this.m_state == STATE_TOASTER){
                _local1 = Math.floor((TrainOrder.getCostPerCommodity(this.m_order.getCommodityType(), this.m_order.getTrainItemName(), this.m_order.getAction()) * this.m_amountToAccept));
                return (ZLoc.t("Dialogs", "TrainUI_ForNum", {number:_local1}));
            };
            return ("");
        }
        protected function getInfoText():String{
            switch (this.m_state){
                case STATE_ACCEPT:
                    if (((this.m_order) && ((this.m_order.getState() == OrderStates.ACCEPTED)))){
                        if (((this.m_order) && ((this.m_order.getAction() == TrainOrder.SELL)))){
                            return (ZLoc.t("Dialogs", "TrainUI_AcceptedSell_Info", {
                                name:this.fn(),
                                number:this.m_order.getAmountFinal()
                            }));
                        };
                        if (((this.m_order) && ((this.m_order.getAction() == TrainOrder.BUY)))){
                            return (ZLoc.t("Dialogs", "TrainUI_AcceptedBuy_Info", {
                                name:this.fn(),
                                number:this.m_order.getAmountFinal()
                            }));
                        };
                    } else {
                        if (((this.m_order) && ((this.m_order.getState() == OrderStates.DENIED)))){
                            if (((this.m_order) && ((this.m_order.getAction() == TrainOrder.SELL)))){
                                return (ZLoc.t("Dialogs", "TrainUI_DeniedSell_Info", {
                                    name:this.fn(),
                                    number:this.m_order.getAmountProposed()
                                }));
                            };
                            if (((this.m_order) && ((this.m_order.getAction() == TrainOrder.BUY)))){
                                return (ZLoc.t("Dialogs", "TrainUI_DeniedBuy_Info", {
                                    name:this.fn(),
                                    number:this.m_order.getAmountProposed()
                                }));
                            };
                        } else {
                            if (((this.m_order) && ((this.m_order.getState() == OrderStates.PENDING)))){
                                if (((this.m_order) && ((this.m_order.getAction() == TrainOrder.SELL)))){
                                    return (ZLoc.t("Dialogs", "TrainUI_AcceptBuy_Info", {
                                        name:this.fn(),
                                        number:this.m_order.getAmountProposed()
                                    }));
                                };
                                if (((this.m_order) && ((this.m_order.getAction() == TrainOrder.BUY)))){
                                    return (ZLoc.t("Dialogs", "TrainUI_AcceptSell_Info", {
                                        name:this.fn(),
                                        number:this.m_order.getAmountProposed()
                                    }));
                                };
                            };
                        };
                    };
                    break;
                case STATE_TOASTER:
                    if (((this.m_order) && ((this.m_order.getAction() == TrainOrder.SELL)))){
                        return (ZLoc.t("Dialogs", "TrainUI_ToasterBuy_Info", {number:this.m_amountToAccept}));
                    };
                    if (((this.m_order) && ((this.m_order.getAction() == TrainOrder.BUY)))){
                        return (ZLoc.t("Dialogs", "TrainUI_ToasterSell_Info", {number:this.m_amountToAccept}));
                    };
                    break;
            };
            return ("");
        }
        protected function getAcceptButtonText():String{
            switch (this.m_state){
                case STATE_ACCEPT:
                    if (((this.m_order) && ((this.m_order.getState() == OrderStates.ACCEPTED)))){
                        return (ZLoc.t("Dialogs", "TrainUI_AcceptComplete_AcceptButton"));
                    };
                case STATE_TOASTER:
                    if (((this.m_order) && ((this.m_order.getAction() == TrainOrder.SELL)))){
                        return (ZLoc.t("Dialogs", "TrainUI_ToasterBuy_AcceptButton"));
                    };
                    if (((this.m_order) && ((this.m_order.getAction() == TrainOrder.BUY)))){
                        return (ZLoc.t("Dialogs", "TrainUI_ToasterSell_AcceptButton"));
                    };
                    break;
            };
            return ("");
        }
        protected function onAccept(_arg1:Event):void{
            var _local2:CustomButton;
            if ((_arg1.currentTarget is CustomButton)){
                _local2 = (_arg1.currentTarget as CustomButton);
                _local2.removeActionListener(this.onAccept);
            };
            if (((!(this.m_trainSim)) || (!(this.m_order)))){
                this.kill();
            };
            if (this.m_order.getState() != OrderStates.PENDING){
                this.m_trainSim.accepted(this.m_order, this.m_order.getAmountFinal(), this.m_uiHandle);
                this.kill();
                return;
            };
            switch (this.m_state){
                case STATE_TOASTER:
                    if (!this.checkAmount(this.m_amountToAccept)){
                        break;
                    };
                    this.m_trainSim.accepted(this.m_order, this.m_amountToAccept, this.m_uiHandle);
                    this.kill();
                    break;
                case STATE_ACCEPT:
                    if (!this.checkAmount(1)){
                        break;
                    };
                    this.m_state = STATE_TOASTER;
                    this.toggleInnerPane();
                    break;
            };
        }
        protected function buildAndShowInnerPane():void{
            this.rebuildInner();
            this.showInnerPane();
        }
        public function toggleInnerPane():void{
            if (m_sliderActive){
                TweenLite.to(m_innerSprite, 0.5, {
                    x:m_offXPos,
                    onComplete:this.buildAndShowInnerPane
                });
            };
        }
        public function backOnePanel():void{
            if (this.m_state == STATE_TOASTER){
                this.m_state = STATE_ACCEPT;
                this.toggleInnerPane();
            };
        }
        protected function onLater(_arg1:Event):void{
            var _local2:CustomButton;
            if ((_arg1.currentTarget is CustomButton)){
                _local2 = (_arg1.currentTarget as CustomButton);
                _local2.removeActionListener(this.onLater);
            };
            this.m_trainSim.later(this.m_order, this.m_uiHandle);
            this.kill();
        }
        protected function onDecline(_arg1:Event):void{
            var _local2:CustomButton;
            if ((_arg1.currentTarget is CustomButton)){
                _local2 = (_arg1.currentTarget as CustomButton);
                _local2.removeActionListener(this.onDecline);
            };
            this.m_trainSim.declined(this.m_order, this.m_uiHandle);
            this.kill();
        }
        public function onMore(_arg1:Event):void{
            if (this.m_order){
                if ((this.m_amountToAccept + Global.gameSettings.getInt("trainSliderAmount", 1)) > this.m_order.getAmountProposed()){
                    return;
                };
                if (!this.checkAmount((this.m_amountToAccept + Global.gameSettings.getInt("trainSliderAmount", 1)))){
                    return;
                };
                this.m_amountToAccept = (this.m_amountToAccept + Global.gameSettings.getInt("trainSliderAmount", 1));
                this.m_trainSim.updateAmountAccepted(this.m_order, this.m_amountToAccept, this.m_uiHandle);
                this.m_amountLabel.setText(("" + this.m_amountToAccept));
                prepareWindow();
                this.m_info.setText(this.getInfoText());
                this.m_info2.setText(this.getInfoText2());
            };
        }
        public function onLess(_arg1:Event):void{
            if (this.m_amountToAccept >= Global.gameSettings.getInt("trainSliderAmount", 1)){
                this.m_amountToAccept = (this.m_amountToAccept - Global.gameSettings.getInt("trainSliderAmount", 1));
                this.m_trainSim.updateAmountAccepted(this.m_order, this.m_amountToAccept, this.m_uiHandle);
                this.m_amountLabel.setText(("" + this.m_amountToAccept));
                this.m_info.setText(this.getInfoText());
                this.m_info2.setText(this.getInfoText2());
                prepareWindow();
            };
        }
        protected function checkAmount(_arg1:int):Boolean{
            var _local2:int;
            var _local3:Boolean;
            var _local4:Boolean;
            var _local5:int;
            if (this.m_order.getAction() == TrainOrder.SELL){
                _local2 = (this.m_order.getCoinCostProposed() / this.m_order.getAmountProposed());
                _local3 = ((_local2 * _arg1) <= Global.player.gold);
                _local4 = (Global.player.commodities.getSpareCapacity(this.m_order.getCommodityType()) >= _arg1);
                if (!_local3){
                    UI.displayMessage(ZLoc.t("Dialogs", "TrainUI_NotEnoughMoney"));
                    return (false);
                };
                if (!_local4){
                    UI.displayMessage(ZLoc.t("Dialogs", "TrainUI_NotWithinCapacity", {item:this.m_order.getCommodityType()}));
                };
            } else {
                if (this.m_order.getAction() == TrainOrder.BUY){
                    _local5 = Global.player.commodities.getCount(this.m_order.getCommodityType());
                    if (_arg1 > _local5){
                        UI.displayMessage(ZLoc.t("Dialogs", "TrainUI_NotEnoughCommodity", {item:this.m_order.getCommodityType()}));
                        return (false);
                    };
                };
            };
            return (true);
        }

    }
}//package Display.TrainUI 
